package org.dromara.dp.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.dp.config.NodeMapConfig;
import org.dromara.dp.vo.*;
import org.dromara.system.service.ICriticalAlarmRiskEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 关键报警模型
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/criticalAlarmModel")
public class CriticalAlarmModelController {

    @Autowired
    private NodeMapConfig nodeMapConfig;

    @Autowired
    private ICriticalAlarmRiskEventService criticalAlarmRiskEventService;

    /**
     * 获取关键报警模型状态
     *
     * @param type 1最近8小时 2最近一天 3最近一周 4近一月
     * @return
     */
    @GetMapping("/riskStatus")
    public R<List<RiskStatusVo>> riskStatus(@RequestParam Integer type) {
        List<RiskStatusVo> res = new ArrayList<>();
        if (type == 1) {
            res.add(new RiskStatusVo(1629400123456789034L, "甲醇缓冲罐", 1, 1, 1));
            res.add(new RiskStatusVo(1629400123456789034L, "二氧化碳压缩机", 2, 2, 0));
            res.add(new RiskStatusVo(1629400123456789034L, "富碳排空设备", 3, 2, 0));
            res.add(new RiskStatusVo(1629400123456789034L, "甲醇输送泵", 1, 0, 0));
            res.add(new RiskStatusVo(1629400123456789034L, "970102流量调节阀", 2, 1, 0));
        } else if (type == 2) {
            res.add(new RiskStatusVo(1629400123456789034L, "甲醇缓冲罐", 2, 2, 1));
            res.add(new RiskStatusVo(1629400123456789034L, "二氧化碳压缩机", 8, 7, 0));
            res.add(new RiskStatusVo(1629400123456789034L, "富碳排空设备", 9, 6, 0));
            res.add(new RiskStatusVo(1629400123456789034L, "甲醇输送泵", 2, 0, 0));
            res.add(new RiskStatusVo(1629400123456789034L, "970102流量调节阀", 3, 2, 0));
        } else if (type == 3) {
            res.add(new RiskStatusVo(1629400123456789034L, "甲醇缓冲罐", 15, 10, 1));
            res.add(new RiskStatusVo(1629400123456789034L, "二氧化碳压缩机", 20, 15, 0));
            res.add(new RiskStatusVo(1629400123456789034L, "富碳排空设备", 50, 45, 0));
            res.add(new RiskStatusVo(1629400123456789034L, "甲醇输送泵", 3, 0, 0));
            res.add(new RiskStatusVo(1629400123456789034L, "970102流量调节阀", 4, 2, 0));
        } else {
            res.add(new RiskStatusVo(1629400123456789034L, "甲醇缓冲罐", 30, 15, 1));
            res.add(new RiskStatusVo(1629400123456789034L, "二氧化碳压缩机", 70, 60, 0));
            res.add(new RiskStatusVo(1629400123456789034L, "富碳排空设备", 60, 45, 0));
            res.add(new RiskStatusVo(1629400123456789034L, "甲醇输送泵", 4, 0, 0));
            res.add(new RiskStatusVo(1629400123456789034L, "970102流量调节阀", 6, 3, 0));
        }
        return R.ok(res);
    }

    /**
     * 风险统计
     *
     * @param type 1最近8小时 2最近一天 3最近一周 4近一月
     * @return
     */
    @GetMapping("/riskSummary")
    public R<List<RiskSummaryVo>> riskSummary(@RequestParam Integer type) {
        List<RiskSummaryVo> res = new ArrayList<>();
        if (type == 1) {
            res.add(new RiskSummaryVo("LV-970101", "58%"));
            res.add(new RiskSummaryVo("LI-970303", "49%"));
            res.add(new RiskSummaryVo("GT-970045", "48%"));
            res.add(new RiskSummaryVo("GT-972417", "44%"));
            res.add(new RiskSummaryVo("TI-972019B", "4%"));
            res.add(new RiskSummaryVo("LT-970101", "4%"));
        } else if (type == 2) {
            res.add(new RiskSummaryVo("LT-970101", "35%"));
            res.add(new RiskSummaryVo("LV-970101", "30%"));
            res.add(new RiskSummaryVo("P9701", "20%"));
            res.add(new RiskSummaryVo("PV970101A", "8%"));
            res.add(new RiskSummaryVo("PV970101B", "4%"));
            res.add(new RiskSummaryVo("PT970101", "3%"));
        } else if (type == 3) {
            res.add(new RiskSummaryVo("GT-970045", "35%"));
            res.add(new RiskSummaryVo("GT-972417", "27%"));
            res.add(new RiskSummaryVo("PI-972007B", "15%"));
            res.add(new RiskSummaryVo("PI-972001A", "8%"));
            res.add(new RiskSummaryVo("PDI-972009A", "7%"));
            res.add(new RiskSummaryVo("GT-972402", "4%"));
            res.add(new RiskSummaryVo("TI-972019B", "2%"));
            res.add(new RiskSummaryVo("LT-970101", "2%"));
        } else {
            res.add(new RiskSummaryVo("GT-970045", "36%"));
            res.add(new RiskSummaryVo("GT-972417", "28%"));
            res.add(new RiskSummaryVo("PI-972007B", "16%"));
            res.add(new RiskSummaryVo("PI-972001A", "7%"));
            res.add(new RiskSummaryVo("PDI-972009A", "7%"));
            res.add(new RiskSummaryVo("GT-972402", "3%"));
            res.add(new RiskSummaryVo("TI-972019B", "2%"));
            res.add(new RiskSummaryVo("LT-970101", "1%"));
        }
        return R.ok(res);
    }

    /**
     * 报警点位列表
     *
     * @param query
     * @return
     */
    @GetMapping("/pageAlarmIndicator")
    public R<Page<RiskAlarmVo>> pageAlarmIndicator(PageQuery query) {
        //5橙 4紫 3红 2 黄 1蓝 0灰
        Page<RiskAlarmVo> page = new Page<>(query.getPageNum(), query.getPageSize());
        List<RiskAlarmVo> records = new ArrayList<>();
        RiskAlarmVo riskAlarmVo8 = new RiskAlarmVo(9L, "LV-970101", 5, "35%", 4, LocalDateTime.of(LocalDate.now(), LocalTime.of(8, 40)), 60L, "进料调节阀故障，报警值持续升高超阈值", "LICA970101", "LICA970101（液位指示调节报警仪表设备）故障");
        RiskAlarmVo riskAlarmVo22 = new RiskAlarmVo(32L, "LT-970101", 4, "50%", 3, LocalDateTime.of(LocalDate.now(), LocalTime.of(8, 50)), 60L, "甲醇缓冲罐液位过高，报警值持续升高超阈值50%。", "LICA970101", "LICA970101（液位指示调节报警仪表设备）故障");
        records.add(riskAlarmVo8);
        records.add(riskAlarmVo22);
        page.setRecords(records);
        page.setTotal(records.size());
        return R.ok(page);
    }


    /**
     * 节点图
     *
     * @param equipmentId
     * @return
     */
    @GetMapping("/nodeMap")
    public R<NodeMapVo> nodeMap(@RequestParam Long equipmentId) {
        NodeMapVo nodeMapVo = criticalAlarmRiskEventService.buildNodeMap(equipmentId);
        return R.ok(nodeMapVo);
//        if (!nodeMapConfig.containsEquipment(equipmentId)) {
//            return R.fail("未找到对应的设备信息");
//        }
//        return R.ok(nodeMapConfig.getNodeMap(equipmentId));
    }

    /**
     * 链路报警
     *
     * @param equipmentId 设备id
     * @return
     * @Param date  日期
     * @Param code  位号
     */
    @GetMapping("/chainAlarm")
    public R<List<NodeAlarmVo>> chainAlarm(Long equipmentId, LocalDate date, String code) {
        RiskAlarmVo riskAlarmVo6 = new RiskAlarmVo(7L, "LV-970101", 2, "15%", 1, LocalDateTime.of(LocalDate.now(), LocalTime.of(8, 35)), 60L, "进料调节阀故障，报警值超阈值15%。", "LICA970101", "LICA970101（液位指示调节报警仪表设备）故障");
        RiskAlarmVo riskAlarmVo8 = new RiskAlarmVo(9L, "LV-970101", 3, "35%", 4, LocalDateTime.of(LocalDate.now(), LocalTime.of(8, 40)), 60L, "进料调节阀故障，报警值持续升高超阈值", "LICA970101", "LICA970101（液位指示调节报警仪表设备）故障");
        RiskAlarmVo riskAlarmVo2 = new RiskAlarmVo(3L, "LT-970101", 2, "10%", 3, LocalDateTime.of(LocalDate.now(), LocalTime.of(8, 40)), 60L, "甲醇缓冲罐液位过高，报警值超阈值10%。", "LICA970101", "LICA970101（液位指示调节报警仪表设备）故障");
        RiskAlarmVo riskAlarmVo21 = new RiskAlarmVo(31L, "LT-970101", 3, "35%", 3, LocalDateTime.of(LocalDate.now(), LocalTime.of(8, 45)), 60L, "甲醇缓冲罐液位过高，报警值持续升高超阈值35%。", "LICA970101", "LICA970101（液位指示调节报警仪表设备）故障");
        RiskAlarmVo riskAlarmVo22 = new RiskAlarmVo(32L, "LT-970101", 4, "50%", 3, LocalDateTime.of(LocalDate.now(), LocalTime.of(8, 50)), 60L, "甲醇缓冲罐液位过高，报警值持续升高超阈值50%。", "LICA970101", "LICA970101（液位指示调节报警仪表设备）故障");
        RiskAlarmVo riskAlarmVo23 = new RiskAlarmVo(33L, "LT-970101", 1, "50%", 3, LocalDateTime.of(LocalDate.now(), LocalTime.of(8, 50)), 60L, "甲醇缓冲罐液位过高，报警值持续升高超阈值50%。", "LICA970101", "LICA970101（液位指示调节报警仪表设备）故障");

        if (equipmentId != null) {
            NodeAlarmVo v1 = new NodeAlarmVo();
            v1.setNodeId(8L);
            v1.setRiskAlarm(riskAlarmVo6);

            NodeAlarmVo v2 = new NodeAlarmVo();
            v2.setNodeId(8L);
            v2.setRiskAlarm(riskAlarmVo8);

            NodeAlarmVo v3 = new NodeAlarmVo();
            v3.setNodeId(12L);
            v3.setRiskAlarm(riskAlarmVo2);

            NodeAlarmVo v4 = new NodeAlarmVo();
            v4.setNodeId(12L);
            v4.setRiskAlarm(riskAlarmVo21);

            NodeAlarmVo v5 = new NodeAlarmVo();
            v5.setNodeId(12L);
            v5.setRiskAlarm(riskAlarmVo22);

//            NodeAlarmVo v6 = new NodeAlarmVo();
//            v6.setNodeId(9L);
//            v6.setRiskAlarm(riskAlarmVo23);
            return R.ok(List.of(v1, v2, v3, v4, v5));
        }
        //5橙 4紫 3红 2 黄 1蓝 0灰
        RiskAlarmVo riskAlarmVo = new RiskAlarmVo(1L, "GT-970045", 1, "20.6", 1, LocalDateTime.now().minusHours(2), 10000L, "甲醇缓冲罐液位高高报到连锁值", "LICA970101", "LICA970101（液位指示调节报警仪表设备）故障");
        RiskAlarmVo riskAlarmVo1 = new RiskAlarmVo(2L, "TI-972019B", 3, "78.9", 4, LocalDateTime.now().minusDays(1), 3500L, "二氧化碳压缩机压力高高报到连锁值", "LICA970101", "LICA970101（液位指示调节报警仪表设备）故障");

        RiskAlarmVo riskAlarmVo3 = new RiskAlarmVo(4L, "PI-972007B", 4, "78.0", 4, LocalDateTime.now().minusDays(1), 2500L, " 甲醇缓冲罐液位高高报到连锁值", "LICA970101", "LICA970101（液位指示调节报警仪表设备）故障");
        RiskAlarmVo riskAlarmVo4 = new RiskAlarmVo(5L, "PDI-972009A", 3, "56.0", 3, LocalDateTime.now().minusDays(1), 1500L, " 甲醇缓冲罐液位高高报到连锁值", "LICA970101", "LICA970101（液位指示调节报警仪表设备）故障");
        RiskAlarmVo riskAlarmVo5 = new RiskAlarmVo(6L, "GT-972402", 1, "23.0", 1, LocalDateTime.now().minusDays(1), 5000L, " 甲醇缓冲罐液位高高报到连锁值", "LICA970101", "LICA970101（液位指示调节报警仪表设备）故障");
        RiskAlarmVo riskAlarmVo9 = new RiskAlarmVo(10L, "GT-972417", 1, "23.0", 1, LocalDateTime.of(LocalDate.now(), LocalTime.of(8, 50)), 60000L, "08时50分GT-972417高报警(报警点位 GT-972417 报警值 81.5% 报警阈值 80%)可", "LICA970101", "LICA970101（液位指示调节报警仪表设备）故障");
        RiskAlarmVo riskAlarmVo10 = new RiskAlarmVo(11L, "PI-972001A", 3, "6.3", 3, LocalDateTime.of(LocalDate.now(), LocalTime.of(8, 55)), (long) (60 * 60 + 2 * 60), "08时55分 LT-970101 液位持续上升 LT-970101 高报警(报警点位 LT-970101 报警值 81.5% 报警阈值 80%)可", "LICA970101", "LICA970101（液位指示调节报警仪表设备）故障");
        RiskAlarmVo riskAlarmVo11 = new RiskAlarmVo(12L, "LI-970303", 3, "6.3", 3, LocalDateTime.of(LocalDate.now(), LocalTime.of(8, 55)), (long) (60 * 60 + 2 * 60), "08时55分 LT-970101 液位持续上升 LT-970101 高报警(报警点位 LT-970101 报警值 81.5% 报警阈值 80%)可", "LICA970101", "LICA970101（液位指示调节报警仪表设备）故障");

        RiskAlarmVo riskAlarmVo12 = new RiskAlarmVo(13L, "P9701", 3, "16.3", 3, LocalDateTime.of(LocalDate.now(), LocalTime.of(8, 55)), (long) (60 * 60 + 2 * 60), "08时55分 LT-970101 液位持续上升 LT-970101 高报警(报警点位 LT-970101 报警值 81.5% 报警阈值 80%)可", "LICA970101", "LICA970101（液位指示调节报警仪表设备）故障");
        RiskAlarmVo riskAlarmVo13 = new RiskAlarmVo(14L, "PV970101A", 3, "8.5", 3, LocalDateTime.of(LocalDate.now(), LocalTime.of(8, 55)), (long) (60 * 60 + 2 * 60), "08时55分 LT-970101 液位持续上升 LT-970101 高报警(报警点位 LT-970101 报警值 81.5% 报警阈值 80%)可", "LICA970101", "LICA970101（液位指示调节报警仪表设备）故障");
        RiskAlarmVo riskAlarmVo14 = new RiskAlarmVo(15L, "PV970101B", 3, "9.8", 3, LocalDateTime.of(LocalDate.now(), LocalTime.of(8, 55)), (long) (60 * 60 + 2 * 60), "08时55分 LT-970101 液位持续上升 LT-970101 高报警(报警点位 LT-970101 报警值 81.5% 报警阈值 80%)可", "LICA970101", "LICA970101（液位指示调节报警仪表设备）故障");
        RiskAlarmVo riskAlarmVo15 = new RiskAlarmVo(16L, "PT970101", 3, "20", 3, LocalDateTime.of(LocalDate.now(), LocalTime.of(8, 55)), (long) (60 * 60 + 2 * 60), "08时55分 LT-970101 液位持续上升 LT-970101 高报警(报警点位 LT-970101 报警值 81.5% 报警阈值 80%)可", "LICA970101", "LICA970101（液位指示调节报警仪表设备）故障");

        List<NodeAlarmVo> records = new ArrayList<>();
        if ("LI-970303".equals(code)) {
            NodeAlarmVo v3 = new NodeAlarmVo();
            v3.setNodeId(12L);
            v3.setRiskAlarm(riskAlarmVo11);
            records.add(v3);
        } else if ("LT-970101".equals(code)) {
            NodeAlarmVo v3 = new NodeAlarmVo();
            v3.setNodeId(12L);
            v3.setRiskAlarm(riskAlarmVo2);

            NodeAlarmVo v4 = new NodeAlarmVo();
            v4.setNodeId(12L);
            v4.setRiskAlarm(riskAlarmVo21);

            NodeAlarmVo v5 = new NodeAlarmVo();
            v5.setNodeId(12L);
            v5.setRiskAlarm(riskAlarmVo22);

            records.add(v3);
            records.add(v4);
            records.add(v5);
        } else if ("LV-970101".equals(code)) {
            NodeAlarmVo v1 = new NodeAlarmVo();
            v1.setNodeId(8L);
            v1.setRiskAlarm(riskAlarmVo6);

            NodeAlarmVo v2 = new NodeAlarmVo();
            v2.setNodeId(9L);
            v2.setRiskAlarm(riskAlarmVo8);
            records.add(v1);
            records.add(v2);
        } else if ("GT-970045".equals(code)) {
            NodeAlarmVo v1 = new NodeAlarmVo();
            v1.setNodeId(8L);
            v1.setRiskAlarm(riskAlarmVo);
            records.add(v1);
        } else if ("GT-972417".equals(code)) {
            NodeAlarmVo v1 = new NodeAlarmVo();
            v1.setNodeId(8L);
            v1.setRiskAlarm(riskAlarmVo9);
            records.add(v1);
        } else if ("GT-972402".equals(code)) {
            NodeAlarmVo v1 = new NodeAlarmVo();
            v1.setNodeId(9L);
            v1.setRiskAlarm(riskAlarmVo5);
            records.add(v1);
        } else if ("TI-972019B".equals(code)) {
            NodeAlarmVo v1 = new NodeAlarmVo();
            v1.setNodeId(9L);
            v1.setRiskAlarm(riskAlarmVo1);
            records.add(v1);
        } else if ("PI-972007B".equals(code)) {
            NodeAlarmVo v1 = new NodeAlarmVo();
            v1.setNodeId(11L);
            v1.setRiskAlarm(riskAlarmVo3);
            records.add(v1);
        } else if ("PI-972001A".equals(code)) {
            NodeAlarmVo v1 = new NodeAlarmVo();
            v1.setNodeId(12L);
            v1.setRiskAlarm(riskAlarmVo10);
            records.add(v1);
        } else if ("PDI-972009A".equals(code)) {
            NodeAlarmVo v1 = new NodeAlarmVo();
            v1.setNodeId(13L);
            v1.setRiskAlarm(riskAlarmVo4);
            records.add(v1);
        } else if ("P9701".equals(code)) {
            NodeAlarmVo v1 = new NodeAlarmVo();
            v1.setNodeId(9L);
            v1.setRiskAlarm(riskAlarmVo12);
            records.add(v1);
        } else if ("PV970101A".equals(code)) {
            NodeAlarmVo v1 = new NodeAlarmVo();
            v1.setNodeId(10L);
            v1.setRiskAlarm(riskAlarmVo13);
            records.add(v1);
        } else if ("PV970101B".equals(code)) {
            NodeAlarmVo v1 = new NodeAlarmVo();
            v1.setNodeId(11L);
            v1.setRiskAlarm(riskAlarmVo14);
            records.add(v1);
        } else if ("PT970101".equals(code)) {
            NodeAlarmVo v1 = new NodeAlarmVo();
            v1.setNodeId(13L);
            v1.setRiskAlarm(riskAlarmVo15);
            records.add(v1);
        }
        return R.ok(records);
    }
}
